<template>
	<div class="grayBg">
		<div class="container">
			<div class="zwList">
				<div class="item" v-for="(item,index) in favList" :key="index" @click="showDetail(item)">
					<h3>{{ item.zwinfo.bmmc }}</h3>
					<p><span>招录人数</span> {{ item.zwinfo.zkrs }}</p>
					<p><span>部门代码</span> <i>{{ item.zwinfo.bmdm }}</i></p>
					<p><span>职位代码</span> {{ item.zwinfo.zwdm }}</p>
					<p><span>职位名称</span> {{ item.zwinfo.zwmc }}</p>
					<p>{{item.zwinfo.city}}</p>
					<div class="btnBox">
						<u-button type="success" shape="circle" size="mini" text="收藏职位+"
							@click.native.stop="addFav(item.zwinfo)" v-if="!favIdList.includes(item.zwinfo._id)"></u-button>
						<u-button type="info" shape="circle" size="mini" plain text="取消收藏-"
								@click.native.stop="removeFav(item.zwinfo._id)" v-else></u-button>
					</div>
				</div>
			</div>
		</div>
		
		<div class="fixBottom">
			<div class="container">
				<div class="flexBox">
					<div class="btn fullWidth">
						<u-button type="success" text="职位对比" @click="changePath('./compare')"></u-button>
					</div>
				</div>
			</div>
			
		</div>

		<u-modal
			:show="showLogin" 
			title="体验次数已用尽,请输入口令码" 
			:cancelText="'暂不登录'" 
			:showCancelButton="false"
			:showConfirmButton="false" 
			>
			<view class="slot-confirmButton">
				<div class="modalBtns">
					<div>
						<u--input
						    placeholder="请输入口令码"
						    border="surround"
						    v-model="klcode"
						  ></u--input>
					</div>
					<u-button type="error" text="确定" @click="checkKlm"></u-button>
					<div class="ewmBox">
						<image :src="userInfo.ewm"show-menu-by-longpress="true" mode="widthFix"></image>
						<p>添加助教微信免费帮查<br>或59元购买口令码</p>
					</div>
				</div>
				
			</view>
		</u-modal>
		<div class="loadBox" v-if="loading">
			<u-loading-icon size="36" mode="circle" :vertical="true" color="red" text="加载中..." textSize="20"></u-loading-icon>
		</div>
		<share></share>
		<u-toast ref="uToast"></u-toast>
	</div>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	const db = wx.cloud.database()
	import share from '../../common/share/index.vue';
	export default {
		data() {
			return {
				searchInfo:"",
				showLogin: false,
				klcode:"",
				loading: true,
				status:"loadmore",
				loadEnd: true,
				page: 0,
				list:[],
				cityList:["哈尔滨","齐齐哈尔","牡丹江","佳木斯","大庆","鹤岗","伊春","鸡西","七台河","双鸭山","绥化","黑河","大兴安岭"],
				selectCity:"选择地区",
				showCity: false,
				sortList:['默认排序','招录人数优先'],
				selectSort:"默认排序",
				showSort: false,
				searchVal:"",
				clickSearch:false,
				favList:[],
				favIdList:[],
				openid:"",
				sk_bl_hasLogin: false,
				searchNum: 0,
				userInfo:{
					ewm:"",
					kfList:[
						{tel:"18145507677", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/1.png"},
						{tel:"18145517677", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/2.png"},
						{tel:"18145187677", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/3.png"},
						{tel:"17745108327", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/4.png"},
						{tel:"17745108347", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/5.png"},
						{tel:"19917513673", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/6.png"},
						{tel:"19917513672", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/7.png"},
						{tel:"18045107677", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/8.png"},
						{tel:"17766512982", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/9.png"},
						{tel:"18646042617", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/10.png"},
						{tel:"18249066701", img: "https://www.baixuemianshi.com/zhuanti/gk-gjj/images/kefu/11.png"}
					]
				},
			};
		},
		onShareAppMessage(options) {
			let shareObj = {
			    title: "黑龙江省考-职位匹配",
			    path: '/pages/sk-tools/zwpp/index?name=' + this.name,
			    imageUrl: ''   
			};
			return shareObj
		},
		onShareTimeline(res) {
			let that = this;
			return {
				title: '黑龙江省考-职位匹配',
				query: "name=" + that.name
			};
		},
		onShow() {},
		onPullDownRefresh() {
			console.log('refresh');
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onPageScroll(e) {
			let that = this;
			that.scrollTop = e.scrollTop;
		},
		onLoad(options) {
			
		},
		computed: {
			searchtimes() {
				return this.$store.getters.searchtimes ? this.$store.getters.searchtimes : '';
			},
			userphone() {
				return this.$store.getters.userphone ? this.$store.getters.userphone : '';
			}
		},
		mounted() {
			if (this.searchtimes > 0) {
				this.searchNum = this.searchtimes
			}
			let hLogin = wx.getStorageSync("sk_bl_hasLogin");
			if(hLogin){
				this.sk_bl_hasLogin = true
			}
		},
		watch: {
			
		},
		created() {
			let that = this;
			that.getopenid()
		},
		onShow(){
			let r = Math.floor(Math.random()*this.userInfo.kfList.length)
			this.userInfo.ewm = this.userInfo.kfList[r].img
		},
		methods: {
			...mapActions(['LogSetSearchtimes','LogSetPhone']),
			getopenid(){
				let that = this
				wx.cloud.callFunction({
					name: 'getopenid',
					data: {},
					complete: res => {
						that.openid =res.result.openid
						that.getAllFav()
					}
				})
			},
			checkKlm(){
				let that = this
				wx.cloud.callFunction({
					name: 'get_klm',
					data: {},
					complete: res => {
						if(res.result.klm == that.klcode){
							that.sk_bl_hasLogin = true
							that.showLogin = false
							wx.setStorageSync('sk_bl_hasLogin', true);
						}else{
							uni.showToast({
								icon: "error",
								title: '口令码不正确'
							});
							that.showLogin = true
						}
					}
				})
			},
			showDetail(info){
				if(this.searchNum >= 3 && !this.sk_bl_hasLogin){
					this.showLogin = true
					return 
				}
				this.searchNum += 1
				this.LogSetSearchtimes(this.searchNum);
				uni.navigateTo({
					url: '../zwxq/index?colname=' + info.colname + '&id=' + info.zwid
				})
			},
			changeCity(e){
				this.selectCity = e.value[0]
				this.showCity = false
				this.page = 0
				this.list = []
				this.loading = true
				this.loadEnd = false
				this.status = 'loadmore'
				this.searchByBmmc()
				
			},
			changeSort(e){
				this.selectSort = e.value[0]
				this.showSort = false
				this.page = 0
				this.list = []
				this.loading = true
				this.loadEnd = false
				this.status = 'loadmore'
				this.searchByBmmc()
			},
			searchFuc(){
				let that = this
				if(!that.searchVal){
					that.clickSearch = false
				}else{
					that.clickSearch = true
				}
				that.page = 0
				that.list = []
				that.loading = true
				that.loadEnd = false
				that.status = 'loadmore'
				
				that.searchByBmmc()
			},
			searchByBmmc(){
				let that = this
				wx.cloud.callFunction({
					name: 'search_bmmc',
					data: {
						colname: that.searchInfo.colname,
						keyword: that.searchVal,
						selectSort:that.selectSort,
						city:that.selectCity,
						limit:20,
						page: that.page
					},
					complete: res => {
						if(res.result.length > 0){
							that.list = that.list.concat(res.result)
						}else{
							that.loadEnd = true
							that.status = 'nomore'
						}
						that.loading = false
					}
				})
			},
			changePath(path){
				uni.navigateTo({
					url: path
				})
			},
			addFav(info){
				let that = this
				if(that.favIdList.length >= 10){
					uni.showToast({
						title:"最多收藏10个！",
						icon:"error"
					})
					return
				}
				db.collection('1_hljsk_fav').add({
					data:{
						colname:that.searchInfo.colname,
						zwid:info._id,
						zwinfo:info,
						time: new Date()
					},
					success: res=>{
						that.favIdList.push(info._id)
					}
				})
			},
			getAllFav(){
				let that = this
				db.collection('1_hljsk_fav').where({
					_openid: that.openid
				}).get({
					success:res =>{
						that.favList = res.data
						let lists = that.favList.map(item => item.zwid)
						that.favIdList = lists
						that.loading  = false
					}
				})
			},
			removeFav(id){
				let that = this
				db.collection('1_hljsk_fav').where({
					_openid: that.openid,
					zwid:id
				}).remove({
					success:res =>{
						that.getAllFav()
					}
				})
			}
		},
		components: {share}
	};
</script>

<style lang="scss">
	@import url(./index.css);
</style>
